:: 单独编译 sqlite-cdbc 的 C 语言部分
setlocal
chcp 65001
@echo off
:: 准备工作
:: 准备 sqlite 
set "EXTRACT_TO_FOLDER=cache"
if not exist "%EXTRACT_TO_FOLDER%\" (  
    mkdir "%EXTRACT_TO_FOLDER%"  
)
:: 获取源码的 .h 文件
:: 注意设置文件夹路径(需要和压缩包文件相同)
set "ZIP_FILE_DIR=sqlite-amalgamation-3460000"
if not exist "%EXTRACT_TO_FOLDER%/%ZIP_FILE_DIR%/sqlite3.h" (
    if not exist "%EXTRACT_TO_FOLDER%/%ZIP_FILE_DIR%.zip" (
        echo 正在下载 sqlite3 源码
        echo 如果下载太慢, 手工下载并移动到文件夹[%EXTRACT_TO_FOLDER%]下, 并重命名为 [%ZIP_FILE_DIR%.zip]
        echo 下载链接: https://www.sqlite.org/2024/sqlite-amalgamation-3460000.zip
        curl -o "%EXTRACT_TO_FOLDER%/%ZIP_FILE_DIR%.zip" "https://www.sqlite.org/2024/sqlite-amalgamation-3460000.zip"
        echo 下载成功
    )
    echo 正在解压文件 [%EXTRACT_TO_FOLDER%/%ZIP_FILE_DIR%.zip]
    PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& { Add-Type -A 'System.IO.Compression.FileSystem'; [IO.Compression.ZipFile]::ExtractToDirectory('%EXTRACT_TO_FOLDER%/%ZIP_FILE_DIR%.zip', '%EXTRACT_TO_FOLDER%'); }"
)

:: 准备生成动态链接库文件夹(需要和压缩包文件相同)
set "LIB_FILE_DIR=sqlite-dll-win-x64-3460000"
if not exist "%EXTRACT_TO_FOLDER%/%LIB_FILE_DIR%/sqlite3.dll" (
    if not exist "%EXTRACT_TO_FOLDER%/%LIB_FILE_DIR%.zip" (
        echo 正在下载 sqlite3 动态连接库
        echo 如果下载太慢, 手工下载并移动到文件夹[%ZIP_FILE_DIR%]下, 并重命名为 [%LIB_FILE_DIR%.zip]
        echo 下载链接: https://www.sqlite.org/2024/sqlite-dll-win-x64-3460000.zip
        curl -o "%EXTRACT_TO_FOLDER%/%LIB_FILE_DIR%.zip" "https://www.sqlite.org/2024/sqlite-dll-win-x64-3460000.zip"
        echo 下载成功
    )
    echo 正在解压文件 [%EXTRACT_TO_FOLDER%/%LIB_FILE_DIR%.zip]
    PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& { Add-Type -A 'System.IO.Compression.FileSystem'; [IO.Compression.ZipFile]::ExtractToDirectory('%EXTRACT_TO_FOLDER%/%LIB_FILE_DIR%.zip', '%EXTRACT_TO_FOLDER%/%LIB_FILE_DIR%'); }"
)

set "TARGET_DIR=objs"
set "TARGET_DLL=libsqlite3cj.dll"
if not exist "%TARGET_DIR%\" mkdir "%TARGET_DIR%"
if exist "%TARGET_DIR%\%TARGET_DLL%" del "%TARGET_DIR%\%TARGET_DLL%"

@echo on
:: 生成 dll 动态文件
gcc -I./%EXTRACT_TO_FOLDER%/%ZIP_FILE_DIR% sqlite3_ext_cj.c -L./%EXTRACT_TO_FOLDER%/%LIB_FILE_DIR% -lsqlite3 -shared -fPIC -o %TARGET_DIR%/%TARGET_DLL%
:: gcc -I./sqlite/sqlite-amalgamation-3460000 sqlite3_ext_cj.c -L./sqlite/ -lsqlite3 -shared -fPIC -o objs/libsqlite3cj.dll

@echo off
echo.
echo 已生成 %TARGET_DIR%\%TARGET_DLL%
echo.